package Sort;

public class MergerSort {
    public static void main(String[] args) {
        int arr[] = {8, 4, 5, 7, 1, 3, 6, 2};
        sort(arr);
        arr.toString();

    }

    public static void mergeSort(int[] arr, int left, int right, int[] temp) {
        if (left < right) {
            int mid = (left + right) / 2;
            mergeSort(arr, left, mid, temp);
            mergeSort(arr, mid + 1, right, temp);
            merge(arr, left, right, mid, temp);
            System.out.println(temp);
        }

    }

    public static void merge(int[] arr, int left, int right, int mid, int[] temp) {
        int l = left; //初始化i,左边有序序列的初始索引
        int r = mid + 1;//初始化j,右边有序序列的初始索引
        int t = 0; //指向temp 数组的当前索引
        /**
         * 1.先把左右两边有序的数据按照规则填充到temp数组
         * 直到左右两边的有序序列处理完为止
         */
        while (l <= mid && r <= right) {
            if (arr[l] <= arr[r]) {
                temp[t] = arr[l];
                t++;
                l++;
            } else {
                temp[t] = arr[r];
                t++;
                r++;
            }
        }
        /**
         * 把有剩余的数据的一边的数据
         */
        while (l <= mid) {
            temp[t] = arr[l];
            t++;
            l++;
        }
        while (r <= right) {
            temp[t] = arr[r];
            t++;
            r++;
        }
        /**
         * 将temp数组的数据拷贝到arr
         */
        t = 0;
        int tLeft = left;
        while (tLeft <= right) {
            arr[tLeft] = temp[t];
            t += 1;
            tLeft += 1;
        }

    }

    public static void sort(int[] arr) {
        int left = 0;
        int right = arr.length - 1;
        int[] temp = new int[arr.length];
        mergeSort2(arr, left, right, temp);


    }

    private static void mergeSort2(int[] arr, int left, int right, int[] temp) {
        if (left < right) {
            int mid = left + (right - left) / 2;
            mergeSort2(arr, left, mid, temp);
            mergeSort2(arr, mid + 1, right, temp);
            merge2(arr, left, mid, right, temp);

        }
    }

    private static void merge2(int[] arr, int left, int mid, int right, int[] temp) {
        int l = left;//左初始
        int r = mid + 1;//右初始
        int t = 0;//temp当前
        //当左初始小于等于左边界 并且右初始 小于=右边界
        while (l <= mid && r <= right) {
            //如果左初始小于等于右初始
            if (arr[l]<=arr[r]){
                temp[t]=arr[l];
                t++;
                l++;
            }else {
                temp[t]=arr[r];
                t++;
                r++;
            }
        }
        //剩余的左右都填上去 这个已经没有排序
        while (l<=mid){
            temp[t]=arr[l];
            t++;
            l++;
        }
        while (r<=right){
            temp[t]=arr[r];
            t++;
            r++;
        }
        //把temp 复制到 arr
        //重置temp 的索引
        t = 0;
        //临时索引 从边界开始复制
        int tempLeft=left;
        while (tempLeft<right){
            arr[tempLeft]=temp[t];
            t++;
            tempLeft++;
        }

    }
}

